﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class Login1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim txtarr As Array
            'txtarr = RetriveData("select id,email, FirstName, LastName from Users where email='" & LoginPanel.UserName & "' and password='" & LoginPanel.Password & "' and IsActive=1", 3)
            Dim strPass As String = Decryptdata(LoginPanel.Password)
            txtarr = RetriveData("select id,email, FirstName, LastName, IsActive, ClientType, Username from ptn_Users where email='" & LoginPanel.UserName & "' and password='" & strPass & "'", 6)
            If Not txtarr(0) Is Nothing Then
                If txtarr(4) = True Then
                    Session.Add("UserId", txtarr(0))
                    Session.Add("Email", txtarr(1))
                    Session.Add("DisplayName", txtarr(2) & " " & txtarr(3))
                    Session.Add("Client", txtarr(5))
                    Session.Add("Username", txtarr(0))

                    If Session("CartID") Is Nothing Then
                        Dim txt As Array
                        txt = RetriveData("select CartId, Status from pt_ShoppingCart where IsDeleted=0 and ExpiredOn >= getDate() and (Status='P' or Status='A') and UserId=" & Session("UserID"), 1)
                        Session.Add("CartID", txt(0))
                    End If


                    'now get the session variable and then go to that "link"
                    If Not ViewState("Referrer") Is Nothing Then
                        Response.Redirect(ViewState("Referrer"))
                        ViewState("Referrer") = Nothing
                    Else
                        Response.Redirect("Default.aspx")
                    End If

                Else
                    'lblMsg.Text = "<h4>Your account not yet activated.</h4>"
                    'lblMsg.ForeColor = Drawing.Color.Red
                    'lblMsg.Font.Bold = True
                    'lblMsg.Visible = True
                    Response.Redirect("login.aspx")
                End If
            End If
            'If Authentication(LoginPanel.UserName, LoginPanel.Password) = True Then
            '    Response.Redirect("Default.aspx")
            'End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub LoginPanel_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles LoginPanel.Authenticate
        Try
            Dim txtarr As Array
            'txtarr = RetriveData("select id,email, FirstName, LastName from Users where email='" & LoginPanel.UserName & "' and password='" & LoginPanel.Password & "' and IsActive=1", 3)
            Dim strPass As String = Encryptdata(LoginPanel.Password)
            txtarr = RetriveData("select id,email, FirstName, LastName, IsActive, ClientType, Username from ptn_Users where email='" & LoginPanel.UserName & "' and password='" & strPass & "'", 6)
            If Not txtarr(0) Is Nothing Then
                If txtarr(4) = True Then
                    Session.Add("UserId", txtarr(0))
                    Session.Add("Email", txtarr(1))
                    Session.Add("DisplayName", txtarr(2) & " " & txtarr(3))
                    Session.Add("Client", txtarr(5))
                    Session.Add("Username", txtarr(6))

                    If Session("CartID") Is Nothing Then
                        Dim txt As Array
                        txt = RetriveData("select CartId, Status from ptn_ShoppingCart where IsDeleted=0 and ExpiredOn >= getDate() and (Status='P' or Status='A') and UserId=" & Session("UserID"), 1)
                        Session.Add("CartID", txt(0))
                    End If


                    'now get the session variable and then go to that "link"
                    If Not ViewState("Referrer") Is Nothing Then
                        Response.Redirect(ViewState("Referrer"))
                        ViewState("Referrer") = Nothing
                    Else
                        Response.Redirect("Default.aspx")
                    End If

                Else
                    'lblMsg.Text = "<h4>Your account not yet activated.</h4>"
                    'lblMsg.ForeColor = Drawing.Color.Red
                    'lblMsg.Font.Bold = True
                    'lblMsg.Visible = True
                    Response.Redirect("login.aspx")
                End If
            End If
            'If Authentication(LoginPanel.UserName, LoginPanel.Password) = True Then
            '    Response.Redirect("Default.aspx")
            'End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class